package com.atgugu.flink.chapter11.hive;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lzc
 * @Date 2022/4/12 11:01
 */
public class Flink01_Hive_1 {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Table table = tEnv.fromDataStream(
            env.fromElements(
                Tuple2.of("10", "a"),
                Tuple2.of("11", "b"),
                Tuple2.of("12", "c")
            ),
            $("id"),$("name")
        );
    
        tEnv.createTemporaryView("t", table);
        
        // 参数3: hive-site.xml 配置所在的目录
        
        // 1.创建hiveCatalog
        HiveCatalog hiveCatalog = new HiveCatalog("hive", "gmall", "input/");
        
        // 2. 注册hiveCatalog
        tEnv.registerCatalog("hive", hiveCatalog);
        
        tEnv.useCatalog("hive");
        tEnv.useDatabase("gmall");
        
        
        tEnv.executeSql("insert into person select * from default_catalog.default_database.t");
        
    }
}
